package com.mbti.youmore.controller;


import com.mbti.youmore.common.exception.HistoryNotExistException;
import com.mbti.youmore.common.exception.PostNotExistException;
import com.mbti.youmore.common.exception.UserNotExistsException;
import com.mbti.youmore.entity.resp.ResponseData;
import com.mbti.youmore.service.HistoryService;
import com.mbti.youmore.util.AccessTokenUtils;
import com.mbti.youmore.util.ResponseUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * 浏览记录模块控制器
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequestMapping("/history")
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseData addHistory(@RequestBody Map<String, String> data) {

        int userId = AccessTokenUtils.getUserId();
        try {
            historyService.addHistory(Integer.parseInt(data.get("postId")), userId);
            return ResponseUtil.getSuccessResponse(null);
        } catch (PostNotExistException e) {
            return ResponseUtil.getErrorResponse(601);
        } catch (UserNotExistsException e) {
            return ResponseUtil.getErrorResponse(401);
        }
    }

    @RequestMapping(method = RequestMethod.GET)
    public ResponseData SelectHistory() {
        List<Map<String, Object>> historyList = null;
        int userId = AccessTokenUtils.getUserId();
        try {
            historyList = historyService.selectHistoryByUerId(userId);
            return ResponseUtil.getSuccessResponse(historyList);
        } catch (UserNotExistsException e) {
            return ResponseUtil.getErrorResponse(401);
        }
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseData deleteAllHistory() {
        Integer userId = AccessTokenUtils.getUserId();
        int count = 0;
        try {
            count = historyService.deleteAllHistoryById(userId);
            if (count != 0) return ResponseUtil.getSuccessResponse(null);
            else return ResponseUtil.getErrorResponse(901);
        } catch (UserNotExistsException e) {
            return ResponseUtil.getErrorResponse(401);
        }
    }

    @RequestMapping(path = "/single", method = RequestMethod.DELETE)
    public ResponseData deleteHistoryById(Integer id) {
        Integer userId = AccessTokenUtils.getUserId();
        try {
            int i = historyService.deleteHistoryById(id, userId);
            if (i == -1) {
                return ResponseUtil.getErrorResponse(902);
            }
            return ResponseUtil.getSuccessResponse(null);
        } catch (UserNotExistsException e) {
            return ResponseUtil.getErrorResponse(401);
        } catch (HistoryNotExistException e) {
            return ResponseUtil.getErrorResponse(903);
        }
    }
}
